package com.horizon.manage.endpoint.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.horizon.common.constrants.HorizonResultCodeEnum;
import com.horizon.common.exception.HorizonException;
import com.horizon.common.util.HorizonUtil;
import com.horizon.manage.endpoint.converter.EndpointEvdConverter;
import com.horizon.manage.endpoint.dto.EndpointDTO;
import com.horizon.manage.endpoint.entity.Endpoint;
import com.horizon.manage.endpoint.service.IEndpointService;
import com.horizon.manage.endpoint.vo.EndpointVO;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 * @author zhaoshb
 * @since 2022/12/21 21:15
 */
@RestController
@AllArgsConstructor
@RequestMapping("/horizon/endpoint")
public class EndpointController {

    private IEndpointService endpointService;

    private EndpointEvdConverter endpointEvdConverter;

    @PostMapping("/create")
    @ApiOperation(value = "创建", notes = "创建")
    public R<Long> create(@RequestBody EndpointDTO dto) throws HorizonException {
        Endpoint endpoint = endpointEvdConverter.dtoToEntity(dto);
        endpointService.save(endpoint);
        return R.data(endpoint.getId());
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除", notes = "删除")
    public R<Object> delete(@RequestBody Long endpointId) throws HorizonException {
        boolean result = endpointService.deleteById(endpointId);
        return result ? R.data(null) : R.fail(HorizonUtil.getLocaleMessage(HorizonResultCodeEnum.ENDPOINT_NOT_EXIST));
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新", notes = "更新")
    public R<Object> update(@RequestBody EndpointDTO dto) throws HorizonException {
        Endpoint endpoint = endpointEvdConverter.dtoToEntity(dto);
        boolean result = endpointService.updateById(endpoint);
        return result? R.data(null) : R.fail(HorizonUtil.getLocaleMessage(HorizonResultCodeEnum.ENDPOINT_NOT_EXIST));
    }

    @GetMapping("/list")
    @ApiOperation(value = "分页", notes = "传入cond以及排序条件")
    public R<IPage<EndpointVO>> list(@ApiIgnore @RequestParam Map<String, Object> cond, Query query) {
        IPage<Endpoint> pages = endpointService.page(Condition.getPage(query), Condition.getQueryWrapper(cond, Endpoint.class));
        return R.data(endpointEvdConverter.pageVO(pages));
    }
}
